
package com.aris.voicechecking;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.voicecheck.adapter.DayReportAdapter;

public class DayReportActivity extends ActionBarActivity {

    protected int weeknumber;

    protected int daynumber;

    protected String answered_list;

    protected String result_list;

    protected ListView day_report_list;

    protected DayReportAdapter adapter;

    private RelativeLayout adView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_day_report);

        initial();
        execution();

    }

    @Override
    protected void onResume() {
        super.onResume();
        SharedPreferences sharedpreferences = getSharedPreferences(getString(R.string.user_data),
                Context.MODE_PRIVATE);

        int number = sharedpreferences.getInt(getString(R.string.update_lesson_result), -1);

        if (number != -1) {
            result_list = result_list.substring(0, number - 1) + '1'
                    + result_list.substring(number);
            execution();
            Editor editor = sharedpreferences.edit();
            editor.putInt(getString(R.string.update_lesson_result), -1);
            editor.commit();
        }

    }

    private void initial() {
        day_report_list = (ListView)findViewById(R.id.day_report_list);
        adView = (RelativeLayout)findViewById(R.id.adView);

        weeknumber = getIntent().getExtras().getInt("weeknumber");
        daynumber = getIntent().getExtras().getInt("daynumber");
        answered_list = getIntent().getExtras().getString("pass_status");
        result_list = getIntent().getExtras().getString("done_status");
    }

    private void execution() {

        adView.setBackgroundResource(R.drawable.banner);

        adView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent webviewIntent = new Intent(DayReportActivity.this, WebviewActivity.class);
                startActivity(webviewIntent);
                overridePendingTransition(R.drawable.anim_slide_in_left,
                        R.drawable.anim_slide_out_left);

            }
        });

        adapter = new DayReportAdapter(weeknumber, daynumber, answered_list, result_list);

        day_report_list.setAdapter(adapter);

        // configuration actionbar
        // getSupportActionBar().setTitle(getString(R.string.back));
        ActionBar actionBar = getSupportActionBar();
        ColorDrawable colorDrawable = new ColorDrawable(getResources().getColor(R.color.main_color));
        actionBar.setBackgroundDrawable(colorDrawable);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_HOME_AS_UP);
        View actionbarcustomeview = getLayoutInflater().inflate(R.layout.actionbar_center_title,
                (ViewGroup)(getSupportActionBar().getCustomView()));

        TextView center_title = (TextView)actionbarcustomeview.findViewById(R.id.center_title);
        center_title.setText(getString(R.string.title_activity_day_report));
        center_title.setTextColor(Color.WHITE);

        actionBar.setCustomView(actionbarcustomeview, new ActionBar.LayoutParams(
                ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.MATCH_PARENT,
                Gravity.CENTER));
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        // Intent gohome = new Intent(DayReportActivity.this,
        // MainActivity.class);
        // startActivity(gohome);
        overridePendingTransition(R.drawable.anim_slide_in_right, R.drawable.anim_slide_out_right);
        finish();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.day_report, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
